package main

import (
	"bytes"
	"fmt"
	"log"

	"gopkg.in/webnice/pdu.v1"
)

func main() {
	var incomming = []string{
		// SMS 1 - Standard SMS
		"+CMGR: 1,,\r\n" +
			"07911326040000F0040B911346610089F60000208062917314080CC8F71D14969741F977FD07",

		// SMS 2 - Standard SMS Deliver
		"+CMGR: 1,,25\r\n" +
			"07919740430900F302170B910000000000F0610130910435216101309104552100",

		// SMS 3 - Standard SMS Deliver
		"+CMGR: 1,,\r\n" +
			"07917283010010F5040BC87238880900F10000993092516195800AE8329BFD4697D9EC37",

		// SMS 4 - Standard SMS-DELIVER
		"+CMGR: 1,,\r\n" +
			"07915892000000F0040B915892214365F700007040213252242331493A283D0795C3F33C88FE06C9CB6132885EC6D341EDF27C1E3E97E7207B3A0C0A5241E377BB1D7693E72E",

		// SMS 5 - 3 Concatenated SMS
		"+CMGR: 1,,159\r\n" +
			"07919740430900F3440B910000000000F00008610130915483218C050003210302044200200441043E043C043D0435043D044C044F0020043C043E0438002C000A04180020043D0435002004340430044E04420020043C043D04350020043704300441043D04430442044C000A041E043F044F0442044C00200442044004350432043E04330438002004320020043D043E04470438002C000A042F00200437043D0430044E0020",
		"+CMGR: 1,,95\r\n" +
			"07919740430900F3440B910000000000F00008610130915493214C05000321030320130020044D0442043E0020043C043E04390020043F04430442044C002C000A04180020043F0440043E0434043E043B04360430044E00200438043404420438002E002E002E",
		"+CMGR: 1,,159\r\n" +
			"07919740430900F3440B910000000000F00008610130915483218C050003210301042F0020043F0440043E0434043E043B04360430044E002004410432043E04390020043F04430442044C002C000A042F0020043F0440043E0434043E043B04360430044E00200438043404420438002C000A041800200445043E0442044C0020043F043E0440043E044E00200441043204350440043D04430442044C000A0425043E0442044F",

		// SMS 6 - SMS Default Alphabet
		"+CMGR: 1,,\r\n" +
			"0791448720003023240DD0E474D81C0EBB010000111011315214000BE474D81C0EBB5DE3771B",

		// SMS 7 - Standard SMS
		"+CMGR: 1,,\r\n" +
			"0791534850020200040C915348707795140000108092327123800DD4F29C0E6A97E7F3F0B91C02",

		// SMS 8 - Error SMS
		"+CMGL: 1,,\r\n" +
			"0041000B915121551532F40000631A0A031906200A032104100A032705040A032E05080A043807002B8ACD29A85D9ECFC3E7F21C340EBB41E3B79B1E4EBB41697A989D1EB340E2379BCC02B1C3F27399059AB7C36C3628EC2683C66FF65B5E2683E8653C1D",
	}

	pduCoder := pdu.New().Decoder(messageReceiver)
	for i := range incomming {
		pduCoder.Writer().Write([]byte(incomming[i]))
	}
	pduCoder.Done()
}

func messageReceiver(msg pdu.Message) {
	var out *bytes.Buffer

	out = &bytes.Buffer{}
	defer log.Println(out)

	fmt.Fprintln(out, "New message found")
	if msg.Error() != nil {
		fmt.Fprintf(out, " * Type: %s\n", msg.Type())
		fmt.Fprintf(out, " * Message error: %s\n", msg.Error())
		return
	}

	fmt.Fprintf(out, " * SMSC: %s", msg.ServiceCentreAddress())
	if msg.Type() == pdu.TypeSmsStatusReport {
		fmt.Fprintf(out, " (status report: [%s] %s)", msg.DischargeTime(), msg.ReportStatus())
	}
	fmt.Fprintln(out, "")

	fmt.Fprintf(out, " * From: %s\n", msg.OriginatingAddress())
	fmt.Fprintf(out, " * Type: %s\n", msg.Type())
	fmt.Fprintf(out, " * SMS (%d): %s\n", msg.DataParts(), msg.Data())
}
